package com.kma.summer2012.facade;

import com.kma.summer2012.dao.CrumbsCatalogOfWorkDAO;
import com.kma.summer2012.dao.CrumbsRequestAndWorkDAO;
import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsCatalogOfWork;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class CatalogOfWorksFacadeImpl implements CatalogOfWorksFacade {

    DAOFactory daoFactory;
    @Override
    public List<String> getAllCategories() {
        ArrayList<String> list = new ArrayList<String>();
//        DAOFactory daoFactory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            ArrayList<CrumbsCatalogOfWork> works = (ArrayList<CrumbsCatalogOfWork>) daoFactory.getCrumbsCatalogOfWorkDAO().getAll();
            for (CrumbsCatalogOfWork work : works) {
                if (work.isIsParent() && !work.isDelete()) {
                    list.add(work.getWorkname());
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return list;
    }

    @Override
    public boolean checkForSubcategory(int subCategoryId, CrumbsCatalogOfWork element) {
        if (element.getId() == subCategoryId) {
            return true;
        } else {
            boolean res = false;
//            DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
            CrumbsCatalogOfWorkDAO dao = (CrumbsCatalogOfWorkDAO) daoFactory.getCrumbsCatalogOfWorkDAO();
            try {
                CrumbsCatalogOfWork work1 = dao.getById(element.getId());
                if (work1 != null) {
                    for (CrumbsCatalogOfWork work : work1.getCrumbsCatalogOfWorks()) {
                        if (checkForSubcategory(subCategoryId, work)) {
                            res = true;
                        }
                    }
                }

            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            return res;
        }
    }

    private boolean workItemIsFree(CrumbsCatalogOfWork work) throws Exception {
//        DAOFactory daoFactory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        CrumbsRequestAndWorkDAO dao = (CrumbsRequestAndWorkDAO) daoFactory.getCrumbsRequestAndWorkDAO();
        int id = work.getId();
     /*   ArrayList<CrumbsRequest> list = (ArrayList<CrumbsRequest>) dao.findByWorkId(id);
        for(CrumbsRequest request : list){
            if(request.getCrumbsRequestsState().getId()==6){
                return false;
            }
        }
        return true;

        */
        return dao.workIsFree(id);
    }

    //never used
    @Override
    public void deleteWorkItem(CrumbsCatalogOfWork workItem) {
//        DAOFactory daoFactory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            daoFactory.getCrumbsCatalogOfWorkDAO().delete(workItem);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteWorkById(int id) {
//        DAOFactory daoFactory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            CrumbsCatalogOfWorkDAO dao = (CrumbsCatalogOfWorkDAO) daoFactory.getCrumbsCatalogOfWorkDAO();
            CrumbsCatalogOfWork work = dao.getById(id);
            dao.delete(work);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean deleteAllWorks(Set<CrumbsCatalogOfWork> workSet) {
//        DAOFactory daoFactory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        boolean allDeleted = true;
        try {
            CrumbsCatalogOfWorkDAO dao = (CrumbsCatalogOfWorkDAO) daoFactory.getCrumbsCatalogOfWorkDAO();
            for (CrumbsCatalogOfWork work : workSet) {
                if (work == null)
                    continue;

                if (dao.getSons(work).size() == 0) {
                    if (workItemIsFree(work)) {
                        work.setDelete(true);
                        dao.update(work);
                    } else {
                        allDeleted = false;
                    }
                } else {
                    //allDeleted=deleteAllWorks(dao.getSons(work));
                    if (deleteAllWorks(dao.getSons(work))) {
                        work.setDelete(true);
                        dao.update(work);
                    } else {
                        allDeleted = false;
                    }

                }
            }
        } catch (Exception e) {
            allDeleted = false;
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return allDeleted;
    }

    public void setDaoFactory(DAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }
}
